<div class="AddBillPanel" style="padding-right: 20px">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">比例:</label>
            <div class="layui-input-block">
                <input style="color: red;width: 100px;border: 0" type="text" name="bill_scale" lay-verify="title"
                       class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">买卖:</label>
            <div class="layui-input-block">
                <input type="radio" lay-filter="paymode" name="paymode" value="收" title="收" checked="checked">
                <input type="radio" lay-filter="paymode" name="paymode" value="出" title="出">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">方式:</label>
            <div class="layui-input-block">
                <input type="radio" lay-filter="TSTM" name="TSTM" value="直接交易" title="直接交易" checked="">
                <input type="radio" lay-filter="TSTM" name="TSTM" value="邮箱交易" title="邮箱交易">
                <input type="radio" lay-filter="TSTM" name="TSTM" value="拍卖行" title="拍卖行">
            </div>
        </div>
        <div class="layui-form-item ProceduresPanel">
            <label class="layui-form-label">手续</label>
            <div class="layui-input-block">
                <input type="checkbox" lay-filter="Procedures" name="Procedures" lay-skin="switch" lay-text="全包|不包" />
                <input type="checkbox" lay-filter="Prosite" name="Prosite" lay-skin="switch" lay-text="网站|非网站" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">对象:</label>
            <div class="layui-input-block">
                <input type="text" name="bill_target" lay-verify="title" autocomplete="off" placeholder="交易对方QQ或联系方式"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">金币:</label>
            <div class="layui-input-block">
                <input type="text" onkeyup="SetSum()" name="bill_gold" lay-verify="title" autocomplete="off"
                       placeholder="请输入金币量" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">金额:</label>
            <div class="layui-input-block">
                <input style="color: red;" onkeyup="SetGolds()" type="text" name="bill_sum" lay-verify="title" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">时间</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="bill_time" placeholder="订单时间">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea id="bill_comment" type="text" style="height: 80px" name="bill_comment" lay-verify="title"
                          class="layui-input"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn btn_add_bill_save">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script>
    $(function () {
        $.getJSON("getConfig", function (result) {
            var data = result["data"][0];
            $("input[name='bill_scale']").val("收" + data["scale_sell"] + "  :  出" + data["scale_pay"]);
            $("#bill_time").val(result["times"]);
            $("#bill_comment").val(data["scale_sell"] +" : " +data["scale_pay"]);

        });
    });
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#bill_time'
            , format: 'yyyy-MM-dd HH:mm:ss'
        });
    });
    layui.use('form', function () {
        var form = layui.form;
        form.render();

        form.on('switch(Procedures)', function (obj) {
            var stauts = obj.elem.checked;
            if (stauts) {
                $("input[name='Procedures']").attr("checked", true);
            } else {
                $("input[name='Procedures']").attr("checked", false);
            }
            SetSum();
        });

        form.on('switch(Prosite)', function (obj) {
            var stauts = obj.elem.checked
                , bill_sum = $("input[name='bill_sum']");
            if (stauts) {
                bill_sum.val(bill_sum.val() - 3);
            } else {
                bill_sum.val(bill_sum.val());
            }
        });

    });
    var paymode = "收", TSTM = "直接交易";
    layui.use(['form', 'jquery'], function () {
        var form = layui.form
        form.on('radio(paymode)', function (data) {
            paymode = data.value;
            SetSum();
        });
        form.on('radio(TSTM)', function (data) {
            TSTM = data.value;
            SetSum();
        });
    });

    function SetSum() {
        $.getJSON("getConfig", function (result) {
            $("input[name='bill_gold']").attr("style", "color: black");
            $("input[name='bill_sum']").attr("style", "color: red");
            var data = result["data"][0];
            var Procedures = $("input[name='Procedures']").attr("checked")
                , gold = $("input[name='bill_gold']").val() * 10000
                , shouxu = 0.97;
            if (TSTM == "邮箱交易") {
                shouxu = 0.95;
            }
            if (Procedures == "checked") {
                shouxu = 1;
            }
            if (paymode == "收") {
                var result = Number(gold) * shouxu / Number(data["scale_sell"] * 10000);
                $("input[name='bill_sum']").val(result.toFixed(2));
            } else {
                shouxu = 1;
                if (Procedures == "checked") {
                    shouxu = 0.97;
                }

                var result = Number(gold) * shouxu / Number(data["scale_pay"] * 10000);
                $("input[name='bill_sum']").val(result.toFixed(2));
            }

        });
    }

    function SetGolds() {
        $.getJSON("getConfig", function (result) {
            $("input[name='bill_sum']").attr("style", "color: black");
            $("input[name='bill_gold']").attr("style", "color: red");
            var data = result["data"][0];
            var Procedures = $("input[name='Procedures']").attr("checked")
                , rmb = $("input[name='bill_sum']").val()
                , shouxu = 0.97;
            if (TSTM == "邮箱交易") {
                shouxu = 0.95;
            }
            if (Procedures == "checked") {
                shouxu = 1;
            }
            if (paymode == "收") {
                var result = Number(rmb) * Number(data["scale_sell"]) / shouxu;
                $("input[name='bill_gold']").val(result.toFixed(2));
            } else {
                shouxu = 1;
                if (Procedures == "checked") {
                    shouxu = 0.97;
                }
                var result = Number(rmb) * Number(data["scale_pay"]) * shouxu;
                $("input[name='bill_gold']").val(result.toFixed(2));
            }
        });
    }

    $(".btn_add_bill_save").click(function () {
        var bill_gold = $("input[name='bill_gold']").val()
            , bill_sum = $("input[name='bill_sum']").val()
            , bill_target = $("input[name='bill_target']").val()
            , bill_comment = $("#bill_comment").val()
            , bill_time = $("#bill_time").val()
            , bill_mode = "收入"
            , Procedures = $("input[name='Procedures']").attr("checked")
            , shouxu = 0.97;
        if (bill_gold == "") {
            alert("请填写金币");
            return false;
        }
        if (Procedures != "checked") {
            shouxu = 1;
        }
        $.getJSON("getConfig", function (result) {
            var data = result["data"][0];
            if (paymode == "收") {
                bill_mode = "支出";
                bill_gold = (bill_sum * data["scale_sell"] * shouxu).toFixed(2);

            }
            $.ajax({
                type: "get"
                , url: "AddBill"
                , data: {
                    bill_gold: bill_gold
                    , bill_sum: bill_sum
                    , bill_mode: bill_mode
                    , bill_target: bill_target
                    , bill_TSTM: TSTM
                    , bill_comment: bill_comment
                    , bill_time: bill_time
                }
                , success: function (result) {
                    if (result == "true") {
                        getMsg();
                        layer.msg('添加成功',{time:1000,maxWidth:160});
                        layui.use('table', function () {
                            var table = layui.table;

                            table.reload('allbills', {
                                url: 'getAllBills'
                                , where: {}
                            });
                        });
                        layer.closeAll();
                    } else {
                        layer.msg('添加失败',{time:1000,maxWidth:160});
                    }
                }
            });

        });


    });
</script>